import resolve from '@rollup/plugin-node-resolve';
import commonjs from "@rollup/plugin-commonjs";
import { babel } from '@rollup/plugin-babel';
import { terser } from "rollup-plugin-terser";
import typescript from "rollup-plugin-typescript2";
import pkg from "./package.json";

function config({ plugins = [], output = {} }) {
  return {
    input: "src/index.ts",
    plugins: [
      typescript(),
      resolve(),
      commonjs(),
      babel({ 
        babelrc: false,
        babelHelpers: 'runtime',
        exclude: 'node_modules/**',
        presets: [
          [
            '@babel/preset-env',
            {
              targets: {
                browsers: '> 0.5%, ie >= 11',
              },
              corejs: 3,
            },
          ],
        ],
        plugins: [
          ['@babel/plugin-transform-runtime']
        ]
      }),
      terser({
        compress: {
          pure_funcs: ['console.log'] // 去掉console.log函数
        }
      }),
      ...plugins
    ],
    output: output,
    external: [
      "@al/h5captcha",
      "@al/h5facealive",
      "@al/h5otp",
      "whatwg-request",
      "js-cookie",
      "crypto-js",
      "petty-css-js",
      "md5",
    ],
  };
}

export default [
  config({
    output: {
      format: "esm",
      file: pkg.module,
      exports: "named",
    },
  }),
  config({
    output: {
      format: "umd",
      name: pkg.name,
      file: pkg.unpkg,
      exports: "named",
      globals: {
        "@al/h5captcha": "H5captcha",
        "@al/h5facealive": "H5facealive",
        "@al/h5otp": "H5otp",
        "whatwg-request": "whatwg-request",
        "crypto-js": "crypto-js",
        "petty-css-js": "petty-css-js",
        "js-cookie": "js-cookie",
        md5: "md5",
      },
    },
  }),
];
